#!/bin/bash

exitCodeCheck(){
if [ $1 -ne 0 ]; then
  echo $2 'error shell common line --------------------X !'
  exit $1
else
  echo $2 'success shell common line !'
fi
}

## 跳到该脚本所在目录
basepath=$(cd `dirname $0`; pwd)
cd ${basepath}

## 判断参数个数
if [ $# != 1 ] ; then
echo "need parameters : snappyTool"
exit 1;
fi

snappyTool=$1



if [ ""${snappyTool} = "java" ] ; then

wget http://git.oschina.net/doubleyu/cmm-tools/raw/master/snappy/snappy.jar

elif [ ""${snappyTool} = "c" ] ; then

wget http://git.oschina.net/doubleyu/cmm-tools/raw/master/snappy/Snappy
chmod +x ./Snappy

else

exitCodeCheck 1 "ErrorParameter"

fi


## 创建输出目录
outputFiles="outputFiles/"
mkdir ${outputFiles}
rm ${outputFiles}*

snappy_files=`ls ${basepath}/*.log*`
for sf in $snappy_files
  do
    filename=`echo ${sf} |awk -F "/" '{print $NF}'`
    echo ${filename}

if [ ""${snappyTool} = "c" ] ; then
    ./Snappy 1 ${sf} ${outputFiles}${filename}.log
elif [ ""${snappyTool} = "java" ] ; then
    java -Xmx128m -jar ./snappy.jar ${sf} ${outputFiles}${filename}.log
elif [ "${snappyTool}" = gunzip ] ; then
    gunzip -c ${sf} > ${outputFiles}${filename}.log
else
    exitCodeCheck 1 ErrorDecodeWay
fi

  done

rm ./snappy.jar
rm ./Snappy
echo

exitCodeCheck $? DONE








